#delimit;
clear all;
set more off;

capture log using "10_DAF_Liquidity_Transformation_Regressions.log", replace;
/*******************************************************************************************/;
** INPUT: Cleaned Guidestar data; 
** OUTPUT: Tables and Figures; 
** LAST MODIFIED: JAG, 2/1/2022;
** RUN TIME: <5 minutes using Stata 15 on computer with 2 x 2.60GHz processors and 64GB RAM;  													      	
/*******************************************************************************************/;

use "DAF-Grants combined for regs at DAF-year level.dta", clear;
replace family=0 if family==.;
** Designate controls and fixed effects for regressions;
local controls="daf_size daf_age contr_gr inv_rev_gr family";
local fixed_effects="year naics";
local fixed_effects2="year naics ein";
local std_errors="vce(cluster ein)";

** Generating key ratios and dependent variables;
count if contrib_noncash==.;
count if contrib_all==.;
replace contrib_noncash=0 if contrib_noncash==.;
gen ratio=contrib_noncash/contrib_all;
sum ratio, d;
sum daf_size daf_age contr_gr inv_rev_gr family;
replace num_intl_grants=0 if num_intl_grants==.;
gen num_tot_grants=num_dom_grants+num_intl_grants;
gen ldga=log(dga);

** Summarizing dependent variable;
sum num_dom_grants if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum num_tot_grants if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum ldga if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum vw_dga_p75_lag_contrib_rev if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum vw_dga_lag_contrib_rev if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum vw_dga_p25_lag_contrib_rev if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;

** Repeating for the within observations;
preserve;
keep if num_dom_grants!=. & ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
bys ein: gen dup=_N;
drop if dup==1;
sum num_dom_grants if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum num_tot_grants if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum ldga if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum vw_dga_p75_lag_contrib_rev if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum vw_dga_lag_contrib_rev if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
sum vw_dga_p25_lag_contrib_rev if ratio!=. & daf_size!=. & daf_age!=. & contr_gr!=. & inv_rev_gr!=. & family!=.;
restore;

** Domestic grants;
reghdfe num_dom_grants ratio `controls', absorb(`fixed_effects') `std_errors';
reghdfe num_dom_grants ratio `controls', absorb(`fixed_effects2') `std_errors';
** Total grants (dom + intl.);
reghdfe num_tot_grants ratio `controls', absorb(`fixed_effects') `std_errors';
reghdfe num_tot_grants ratio `controls', absorb(`fixed_effects2') `std_errors';
** Log of total $ of grants;
reghdfe ldga ratio `controls', absorb(`fixed_effects') `std_errors';
reghdfe ldga ratio `controls', absorb(`fixed_effects2') `std_errors';

** SWITCH TO WITHIN FIRM ANALYSIS;
** GOAL IS TO UNDERSTAND WHEN THEY ARE GETTING A GREATER SHARE WHAT IS HAPPENING;
** This alternatively looks at the value-weighted share of grants going to smaller non-profits;
reghdfe vw_dga_p75_lag_contrib_rev ratio `controls', absorb(`fixed_effects') `std_errors';
reghdfe vw_dga_p75_lag_contrib_rev ratio `controls', absorb(`fixed_effects2') `std_errors';

reghdfe vw_dga_lag_contrib_rev ratio `controls', absorb(`fixed_effects') `std_errors';
reghdfe vw_dga_lag_contrib_rev ratio `controls', absorb(`fixed_effects2') `std_errors';

reghdfe vw_dga_p25_lag_contrib_rev ratio `controls', absorb(`fixed_effects') `std_errors';
reghdfe vw_dga_p25_lag_contrib_rev ratio `controls', absorb(`fixed_effects2') `std_errors';

log close;
